﻿<%
class UserRightClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "USER_RIGHT"
    end property

    public userId, blogId, catId 
    public canRead, canReply, canPost, canReadSecret, canDelete, canManage

    private sub class_initialize()
        canRead=false
        canReply=false
        canPost=false
        canDelete=false
        canReadSecret=false
        canManage=false
    end sub
    
    private sub class_terminate()
    
    end sub
    
    
    public function build(array)
        build = false
        if isEmpty(array) or not isArray(array) then
            exit function
        end if
        if ubound(array)<9 then
            exit function
        end if
                
        userId       = array(0)
        blogId       = array(1)
        catId        = array(2)
        canRead      = array(3)
        canReply     = array(4)
        canPost      = array(5)
        canDelete    = array(6)
        canReadSecret= array(7)
        canManage    = array(8)
        
        build = true 
    end function 
        
    public function toArray()
        dim array(9)    
        array(0) = userId       
        array(1) = blogId       
        array(2) = catId        
        array(3) = canRead      
        array(4) = canReply     
        array(5) = canPost      
        array(6) = canDelete    
        array(7) = canReadSecret
        array(8) = canManage    
        
        toArray = array
    end function
    
    public function load(user_id, blog_id, cat_id)
        load = false
        if isEmpty(blog_id) then
            exit function
        end if
        
        userId = user_id
        blogId = clng(blog_id)
        catId  = cat_id

        load = loadFromCache

        if load=false then
            load = loadFromDB()
            if load=true then
                call cache()
            end if    
        end if    
        
    end function
    
    public function reload(user_id, blog_id, cat_id)
        load = false
        if isEmpty(blog_id) then
            exit function
        end if
        
        userId = user_id
        blogId = clng(blog_id)
        catId  = cat_id
        
        reload = loadFromDB()
        if (reload=true) then
            call cache()
        else    
            call clean()
        end if    
    end function
    
    public function clean()
        call removeFromCache(CACHE_PREFIX, userId & "." & blogId & "." & catId)
        userId = Empty
        blogId = Empty
        catId = Empty
        canRead=true
        canReply=true
        canPost=false
        canDelete=false
        canReadSecret=false
        canManage=false
    end function

    
    
    public sub cache()
        'response.write userId & "." & blogId & "." & catId
        if not isEmpty(blogId) then
            call build(putIntoCache(CACHE_PREFIX, userId & "." & blogId & "." & catId, toArray()))
        end if    
    end sub
    

    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, userId & "." & blogId & "." & catId))
    end function

    
    private function loadFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

      call openDB
      'response.write "loadUserRightFromDB"
          
      loadFromDB = false
      dim blog
      set blog = new BlogClass
      blog.load(blogId)
        
      if not isEmpty(userId) and userId=blog.blogOwnerId  then   
          canRead=true
          canReply=true
          canPost=true
          canDelete=true
          canReadSecret=true
          canManage=true
          loadFromDB = true
          exit function
      end if        

        
      sql = " select a.can_read, a.can_reply, a.can_post, a.can_delete, a.can_read_secret, a.can_manage from blog_user_group a " 
      if userId="" then
        '匿名网友
        rs.open sql & " where a.blog_id=" & blogId & " and a.group_id=4 ", conn, 1, 1 

      else
        userId = clng(userId)
        '验证网友
        'rs.open sql & " where a.blog_id=" & blogId & " and group_id in ( select user_group from blog_users where blog_id="  & blogId & " and user_id=" & userId & ") " , conn, 1, 1
        
        rs.open sql & " inner join  blog_users b on a.group_id=b.user_group and a.blog_id=b.blog_id " &_
                      " where a.blog_id=" & blogId & "  and b.user_id=" & userId , conn, 1, 1
      end if
    
      if rs.eof and userId<>"" then
        rs.close()
        '普通网友
        rs.open   sql & " where a.blog_id=" & blogId & " and a.group_id=3 ", conn, 1, 1 
      end if        
    
      dim record_count
      record_count = rs.recordCount
      if record_count<1 then
        '设置默认级别
        canRead=true
        canReply=true
      else
        dim i
        for i=1 to record_count
          if rs("can_read") then
            canRead = True
          end if  
          
          if rs("can_reply") then
            canReply = True
            canRead = True
          end if  
          
          if rs("can_post") then
            canPost = True            
            canReply = True
            canRead = True
          end if  
          
          if rs("can_delete") then
            canDelete = True
            canRead = True
          end if  
          
          if rs("can_read_secret") then
            canReadSecret = true
            canRead = True
          end if  
          
          if rs("can_manage") then
            canManage = True
            canDelete = True
            canReply = True
            canPost = True
            canReadSecret = True
            canRead = True
          end if  
          rs.moveNext()
        next
      end if
      rs.close()    
      set rs = nothing
      
      loadFromDB = true
    end Function
    
    '文章读权限
    public function canReadItem(blogItem)
        canReadItem = False
        If userId<>"" And userId=blogItem.userId Then '作者本人
            canReadItem = True
        ElseIf canRead And (not blogItem.isSecret Or canReadSecret) Then '有普通文章读权限、隐藏文章有专读权限
            canReadItem = True
        End If
    end function    
    
    '文编辑权限
    public function canEditItem(blogItem)
        canEditItem = False
        If blogItem.isRss Then 'rss来源文章不能编辑
            canEditItem = False
        ElseIf userId<>"" And userId=blogItem.userId Then '作者本人
            canEditItem = True
        ElseIf canManage Then 'blog管理员
            canEditItem = True
        End If
    end Function  
    
    
    '文删除权限
    public function canDeleteItem(blogItem)
        canDeleteItem = False
        If blogItem.isRss Then 'rss来源文章不能编辑删除
            canDeleteItem = False
        ElseIf userId<>"" And userId=blogItem.userId Then '作者本人
            canDeleteItem = True
        ElseIf canDelete Then  '有删除权限
            canDeleteItem = True
        End If
    end Function  
        
    '文回复权限
    public function canReplyItem(blogItem)
        canReplyItem = False
        If blogItem.isRss Then 'rss来源文章不能回复
            canReplyItem = False
        ElseIf userId<>"" And userId=blogItem.userId Then '作者本人
            canReplyItem = True
        ElseIf canReply Then  '有回复权限
            canReplyItem = True
        End If
    end Function  
                
                
    '评论读权限
    Public Function canReadComment(blogComment)
        canReadComment = False
        
        If userId<>"" And userId=blogComment.userId Then '评论作者本人
            canReadComment = True
        End If
        
        If  canReadComment = False Then
            Dim parent
            Set parent = blogComment.getParentComment
            If userId<>"" And Not IsEmpty(parent) And userId = parent.userId Then '父评论作者
                canReadComment = True
            End If 
        End If
        
        If  canReadComment = False Then
            Dim blogItem
            Set blogItem = blogComment.getBlogItem
            
            If canRead And (not blogItem.isSecret Or canReadSecret)  And (not blogComment.isSecret Or canReadSecret) Then '有普通文章读权限、隐藏文章有专读权限
                canReadComment = True
            End If 
        End If
    End Function 
    
    '评论编辑权限
    public function canEditComment(blogComment)
        canEditComment = False
        If userId<>"" And userId=blogComment.userId Then '作者本人
            canEditComment = True
        ElseIf canManage Then 'blog管理员
            canEditComment = True
        End If
    end Function  
    
    
    '评论删除权限
    public function canDeleteComment(blogComment)
        canDeleteComment = False
        If userId<>"" And userId=blogItem.userId Then '作者本人
            canDeleteComment = True
        ElseIf canDelete Then  '有删除权限
            canDeleteComment = True
        End If
    end Function  
        
    '评论回复权限
    public function canReplyComment(blogComment)
        canReplyComment = False
        If canReply Then  '有回复权限
            canReplyComment = True
        End If
    end Function  
                                
end class
%>